package com.xx.sg.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.xx.sg.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/weather")
@Tag(name = "天气管理", description = "天气管理")
public class WeatherController {

    @Value("${transfer.weather.http-address}")
    private String HTTP_ADDRESS;
    @Autowired
    private RestTemplate restTemplate;


    @GetMapping("/info")
    @Operation(summary = "查看单个", description = "查看单个")
    public R info(@RequestParam String longitude, @RequestParam String latitude) {

        String url = String.format("%s/nmgsg/getlbs?lon=%s&lat=%s", HTTP_ADDRESS, longitude, latitude);

        // 构建请求头
        HttpHeaders headers = new HttpHeaders();
        // 构建请求实体（GET 没有请求体，这里只传 headers）
        HttpEntity<String> entity = new HttpEntity<>(headers);

        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        JSONObject jsonObject = JSON.parseObject(response.getBody());
        return R.success(jsonObject);
    }
}
